package model

import (
	"gitee.com/xuxinzhi2206/corehub/database/mysql"
	"gorm.io/gorm"
)

// 分类表
type Types struct {
	gorm.Model
	Icon     string `gorm:"column:icon;type:varchar(255);comment:分类图片" json:"icon"`
	Lv       int64  `gorm:"column:lv;type:tinyint(1);comment:分类等级"     json:"lv"`
	Pid      int64  `gorm:"column:pid;type:int(11);comment:父级id"        json:"pid"`
	Name     string `gorm:"column:name;type:varchar(255)"                json:"name"`
	Location int64  `gorm:"column:location;type:tinyint(2);commit:1分类1,2分类2,0全部分类"`
}

func (t *Types) GetTypeLv1ByID(id int64) (types *Types, err error) {
	err = mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("id = ?", id).Find(&types).Error
	})
	return
}

func (t *Types) GetTypeLv2ByPID(pid int64) (types []*Types, err error) {
	err = mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("pid = ?", pid).Find(&types).Error
	})
	return
}

func (t *Types) GetTypeListByIDs(ids []int64) (types []*Types, err error) {
	err = mysql.WithMysql(func(db *gorm.DB) error {
		return db.Where("pid IN (?)", ids).Find(&types).Error
	})
	return
}

func (t *Types) GetTypeList() (types []*Types, err error) {
	err = mysql.WithMysql(func(db *gorm.DB) error {
		return db.Find(&types).Error
	})
	return
}

func (t *Types) GetTypeByLocation(location int64) (types []*Types, err error) {
	err = mysql.WithMysql(func(db *gorm.DB) error {
		if location != 0 {
			db = db.Where("location = ?", location)
		}
		return db.Find(&types).Error
	})
	return
}
